#!/usr/bin/perl

#use English;
use File::Basename;
use File::Compare;
use File::Copy;

my $DUP_PATH = "/Users/sparkyr/Pictures/dup";
my $BASE_SAVEPATH = "/Users/sparkyr/Pictures";
my @EXTENSION_LIST = (".CR2", ".JPG", ".MOV", ".PNG", ".jpg", ".mp4");

# Find everything that looks like a duplicated import
my @backup_files = `find $BASE_SAVEPATH -name "*-00?.*"`;
chomp @backup_files;

foreach my $this_file (@backup_files) {
#	print "$this_file\n";
	my $name, $path, $suffix;
	($name, $path, $suffix) = fileparse($this_file, @EXTENSION_LIST);
	
	# Figure out what the base name ought to be
	my $reduced_name = ($name =~ m/[^\d]\d+$/, $`) . $suffix;
	print "--> $reduced_name\n";

	# Look everywhere for matching originals
	my @find_originals = `find $BASE_SAVEPATH -name $reduced_name`;
	chomp @find_originals;
	my $num_originals = @find_originals;

	if ($num_originals >= 1) {
#		print "Loop over @find_originals\n";
		foreach my $orig_file (@find_originals) {
			
			# See if it's okay to delete the redundant file
			if (compare($this_file, $orig_file) == 0) {
				print "$this_file == $orig_file\n";
				# delete backup
#				move($this_file, $DUP_PATH) || die "Can't move $this_file to dup\n";
				last;
			}

		}
	}
}
